package com.huiyujinrong.qiangdanbao.activity.fragment;


import android.content.Context;

import com.huiyujinrong.qiangdanbao.BorrowHelpApp;
import com.huiyujinrong.qiangdanbao.DaggerModelComponent;
import com.huiyujinrong.qiangdanbao.bean.FirstTradeRecord;
import com.huiyujinrong.qiangdanbao.bean.UserStatusBean;
import com.huiyujinrong.qiangdanbao.model.FragmentMyModel;
import com.huiyujinrong.qiangdanbao.network.Api;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import happyloan.core.RespEvent;

public class FragmentMyPresenter implements FragmentMyContract.Presenter {

    @Inject
    FragmentMyModel model;
    private FragmentMyContract.View view;
    private Context context;

    public FragmentMyPresenter(FragmentMyContract.View v, Context context) {
        this.view = v;
        this.context = context;
        setupDagger2();
    }

    public void setupDagger2() {
        DaggerModelComponent.builder().appComponent(BorrowHelpApp.get().getAppComponent()).build().inject(this);
    }

    @Override
    public void selectUserStatus(String user_id) {
        model.selectUserStatus(user_id);
    }


    @Override
    public void start() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void end() {
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void selectUserStatus(RespEvent<UserStatusBean> event) {
        if (event.getUrl().equals(Api.selectUserStatus)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    //数据返回成功
                    UserStatusBean bean = event.getT();
                    view.setViewData(bean);
                    break;
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    if (event.isAlert()) {
                        view.showToast(String.valueOf(event.getExtra()));
                    }
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    view.showToast("网络不好,稍后再试");
                    break;
            }
        }
    }



}
